package xupt.se.ttms.view.tmpl;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import xupt.se.ttms.model.Customer;
import xupt.se.ttms.model.Employee;
import xupt.se.ttms.service.CustomerSrv;
import xupt.se.ttms.service.EmployeeSrv;
import xupt.se.ttms.view.customer.Customer_MainUITmpl;
import xupt.se.ttms.view.customer.Customer_Register;
import xupt.se.ttms.view.employee.Employee_MainUITmpl;

public class Login{

	private JTextField username;
    // 密码
    private JPasswordField password;
    private JLabel jo5;
    // 小容器
    private JLabel jl1;
    private JLabel jl2;
    private JButton jl3;
    private JButton jl4;
    private JLabel jl5;
    private JLabel jl6;
    // 小按钮
    private JButton bu1;
    private JButton bu3;
    public JFrame frame;

    // 复选框
    private JCheckBox jc1;
    private JCheckBox jc2;

    // 列表框
    private JComboBox jcb;

    /*
     * 构造方法
     */
    public Login() {
        // 设置窗口标题
    	frame=new JFrame();
    	frame.setTitle("小霸王影院");
        // 窗体组件初始化
        init();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置布局方式为绝对定位
        frame.setLayout(null);

        frame.setBounds(0, 0, 460,290);
        // 设置窗体的标题图标
        Image image = new ImageIcon("img/12.jpg").getImage();
        frame.setIconImage(image);
        
        // 窗体大小不能改变
        frame.setResizable(false);

        // 居中显示
        frame.setLocationRelativeTo(null);

        // 窗体可见
        frame.setVisible(true);
    }

    /*
     * 初始化方法
     */
    public void init() {
        // 创建一个容器
        Container con = frame.getContentPane();
        con.setBackground(Color.decode("#FAF0E6"));
        jl1 = new JLabel();
        // 设置背景图片
        jo5= new JLabel("  小霸王剧院票务管理系统");
        jo5.setFont(new Font("楷体", Font.BOLD, 32));
        jo5.setForeground(Color.decode("#FF4500"));
        jo5.setBackground(Color.decode("#EECBAD"));
        jo5.setOpaque(true);
        jo5.setBounds(0,0,455, 90);
        Image image1 = new ImageIcon("img/16.jpg").getImage();
        jl1.setIcon(new ImageIcon(image1));
        jl1.setBounds(0, 0, 460, 280);
        
        // QQ登录头像设定
        jl2 = new JLabel();
       
        jl2.setBounds(0,185, 90, 90);

        // 用户号码登录输入框
        username = new JTextField();
        username.setBounds(120, 100, 150, 20);
        // 用户号码登录输入框旁边的文字
        jl3 = new JButton("用户注册");
        jl3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				Customer_Register s =new Customer_Register();
			}
		});
        jl3.setForeground(Color.BLUE);
        jl3.setBorderPainted(false);
        jl3.setContentAreaFilled(false);
        jl3.setBounds(258, 100, 100, 20);

        // 密码输入框
        password = new JPasswordField();
        password.setBounds(120, 130, 150, 20);
        // 密码输入框旁边的文字
        jl4 = new JButton("找回密码");
        jl4.setForeground(Color.BLUE);
        jl4.setBorderPainted(false);
        jl4.setContentAreaFilled(false);
        jl4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				System.out.print("找回");
			}
		});
        jl4.setBounds(258, 130, 100, 20);
        jl5 = new JLabel("账号:");
        jl5.setBounds(75,100,55,20);
        jl6 = new JLabel("密码:");
        jl6.setBounds(75,130,55,20);
        // 输入框下方文字
        jc1 = new JCheckBox("记住密码");
        jc1.setBounds(115, 155, 80, 15);
        jc2 = new JCheckBox("自动登录");
        jc2.setBounds(195, 155, 80, 15);
        // 用户登录状态选择
        jcb = new JComboBox();
        jcb.addItem("员工");
        jcb.addItem("顾客");
        jcb.setBounds(280, 153, 75, 20);

        // 按钮设定
        bu1 = new JButton("登录");
        bu1.setFont(new Font("微软雅黑", Font.BOLD, 28));
        bu1.setBackground(Color.decode("#EECBAD"));
        bu1.setOpaque(true);
       
        bu1.setBounds(105, 200, 200, 40);
        
        // 给按钮添加1个事件
        bu1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String str=e.getActionCommand();
                if("登录".equals(str)){
                    String getName =username.getText();
                    String getPwd =password.getText();
                    if (getName.equals("")|| getPwd.equals("")) {
						JOptionPane.showConfirmDialog(null, "输入不能为空");
					}
                    else {
                    	  //员工Employee
                        if(jcb.getSelectedIndex() == 0){
                        	  EmployeeSrv employeeSrv=new EmployeeSrv();
                              try {
          						List<Employee> employees=employeeSrv.Fetch(getName);
          						if(employees.size()==0)
          						{
          							JOptionPane.showConfirmDialog(null, "输入有误");
          						}
          						else {
          							if(employees.get(0).getPassword().equals(getPwd))
          							{
          								Employee_MainUITmpl s = new Employee_MainUITmpl(getName);
          	                    		frame.dispose();
          							}
          							else
          							{
          								JOptionPane.showConfirmDialog(null, "密码错误");
          							}
          						}
          					} catch (Exception e1) {
          						// TODO 自动生成的 catch 块
          						e1.printStackTrace();
          					}
                        	
                        }
                        //顾客Customer
                        else if(jcb.getSelectedIndex() == 1)
                        {
                        	  CustomerSrv customerSrv=new CustomerSrv();
                              try {
          						List<Customer> customers=customerSrv.Fetch(getName);
          						if(customers.size()==0)
          						{
          							JOptionPane.showConfirmDialog(null, "输入有误");
          						}
          						else {
          							if(customers.get(0).getCustomer_password().equals(getPwd))
          							{
          								Customer_MainUITmpl s = new Customer_MainUITmpl(getName);
          	                    		frame.dispose();
          							}
          							else
          							{
          								JOptionPane.showConfirmDialog(null, "密码错误");
          							}
          						}
          					} catch (Exception e1) {
          						// TODO 自动生成的 catch 块
          						e1.printStackTrace();
          					}
                        	
                        }
					}
                  
                    //JOptionPane.showConfirmDialog(null, "您输入的用户名是"+jcb.getItemAt( jcb.getSelectedIndex()));
                }

            }
        });
     
        // 所有组件用容器装载
        jl1.add(jo5);
        jl1.add(username);
        jl1.add(password);
        jl1.add(jl2);
        jl1.add(jl3);
        jl1.add(jl4);
        jl1.add(jc1);
        jl1.add(jc2);
        jl1.add(jcb);
        jl1.add(bu1);
        jl1.add(jl5);
        jl1.add(jl6);
        con.add(jl1);
        
    }
    public static void main(String[] args) {
        // 实例化对象
    	Login qq = new Login();
    }
}

